package com.example.cf.grouplist.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.cf.grouplist.R;
import com.example.cf.grouplist.utils.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

public class RefreshActivity extends AppCompatActivity {

    private RecyclerView recycleView;
    private MAdapter mAdapter;
    private List<String> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refresh);
        recycleView = ((RecyclerView) findViewById(R.id.recyclerView));
        initData();
        mAdapter = new MAdapter();
        mAdapter.setItemClickListener(new ItemClickListener() {
            @Override
            public void itemClick(MAdapter.MViewHolder holder, View itemView, int position) {
                Toast.makeText(RefreshActivity.this, data.get(position), Toast.LENGTH_SHORT).show();
            }
        });
        recycleView.setLayoutManager(new GridLayoutManager(this,1));
        recycleView.setAdapter(mAdapter);
        recycleView.addItemDecoration(new RecycleViewDivider(this).setmDivider(getResources().getDrawable(R.drawable.divider)));

    }

    private void initData() {
        data = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            data.add("item" + i);
        }
    }

    public class MAdapter extends RecyclerView.Adapter<MAdapter.MViewHolder> {

        public void setItemClickListener(ItemClickListener itemClickListener) {
            this.itemClickListener = itemClickListener;
        }

        private ItemClickListener itemClickListener;

        @Override
        public MAdapter.MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            if (viewType==RecyclerView.INVALID_TYPE){

            }else if (viewType==RecyclerView.INVALID_TYPE-1){

            }
            return new MViewHolder(LayoutInflater.from(RefreshActivity.this).inflate(R.layout.refresh_item, parent, false));
        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            holder.tv.setText(data.get(position));
            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null) {
                        itemClickListener.itemClick(holder, holder.itemView, position);
                    }
                }
            });
        }




        @Override
        public int getItemCount() {
            return data.size();
        }

        public class MViewHolder extends RecyclerView.ViewHolder {
            TextView tv;
            View itemView;

            public MViewHolder(View itemView) {
                super(itemView);
                this.itemView = itemView;
                tv = ((TextView) itemView.findViewById(R.id.tv));
            }
        }

    }

    public interface ItemClickListener {
        void itemClick(MAdapter.MViewHolder holder, View itemView, int position);
    }

}
